<?php
header("content-type:text/html;charset=utf8");
/**
*
* example目录下为简单的支付样例，仅能用于搭建快速体验微信支付使用
* 样例的作用仅限于指导如何使用sdk，在安全上面仅做了简单处理， 复制使用样例代码时请慎重
* 请勿直接直接使用样例对外提供服务
*
**/
require_once "/weixin/lib/WxPay.Api.php";
require_once "/weixin/example/WxPay.JsApiPay.php";
require_once "/weixin/example/WxPay.Config.php";
require_once '/weixin/example/log.php';

//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);

//打印输出数组信息
function printf_info($data)
{
    foreach($data as $key=>$value){
echo "<font color='#00ff55;'>$key</font> :  ".htmlspecialchars($value, ENT_QUOTES)." <br/>";
}
}
//①、获取用户openid
try{
//echo Cookie("openId");die;
$tools = new JsApiPay();
$openId = Cookie("openId");
//②、统一下单
//$input = new WxPayUnifiedOrder();
//$input->SetBody("test");
//$input->SetAttach("test");
//$input->SetOut_trade_no("sdkphp".date("YmdHis"));
//$input->SetTotal_fee("1");
//$input->SetTime_start(date("YmdHis"));
//$input->SetTime_expire(date("YmdHis", time() + 600));
//$input->SetGoods_tag("test");
//$input->SetNotify_url("http://paysdk.weixin.qq.com/notify.php");
//$input->SetTrade_type("JSAPI");
//$input->SetOpenid($openId);
//$config = new WxPayConfig();

//$order = WxPayApi::unifiedOrder($config, $input);

//echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
//print_r($order);die;
//$jsApiParameters = $tools->GetJsApiParameters($order);

//获取共享收货地址js函数参数
//$editAddress = $tools->GetEditAddressParameters();

} catch(Exception $e) {
//print_r($e);die;
//Log::ERROR(json_encode($e));
}

?>
<input type="hidden" class="openid" value="<?php echo $openId;?>"/>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>全部订单</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    <!--标准mui.css-->
    <link rel="stylesheet" href="__PUBLIC__/css/mui.min.css">
    <!--App自定义的css-->
    <!--<link rel="stylesheet" type="text/css" href="../../css/app.css" />-->
    <style>
        ol{margin: 0;padding: 0;list-style: none}
        .tab{display:flex;flex-direction: row;justify-content:space-between;align-items:center;margin-left: -2.5rem;height: 1.5rem}
        .tab li{ align-items: center;justify-content: center}
        .tab i{width: 100%;height: 2px;background: #2e97f5;position: absolute;bottom: -1.2rem;left: 0;display: none}
        .tab i.on{display: block}
        .active{color: #2e97f5}
        .listBox{display: flex;flex-direction: column;border-bottom: solid 1px #efefef;padding-bottom: 1rem}
        .listBox li{display: flex;flex-direction: row;justify-content: space-between;height: 2rem;align-items: center;margin-right: 1rem;margin-left: -1.2rem}
        .btnBox{display: flex;flex-direction: row;justify-content: flex-end;align-items: center;margin-top: -1rem}
        .btnBox li{padding: 0.3rem 0.5rem;border: solid 1px #CCCCCC; border-radius: 0.2rem;margin: 1rem;font-size: 0.6rem;margin-left: 0}
        .red{color: #e6524e}
        .btnBox li.green{color: #2e97f5;border: solid 1px #2e97f5}
        .btnBox li.gry{color: #666666;border: solid 1px #666666}
        .btnBox li.reded{color: #e6524e;border: solid 1px #e6524e}
    </style>
</head>
<body>
<header class="mui-bar mui-bar-nav">
    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
    <h1 class="mui-title">订单</h1>
</header>
<div class="mui-content">
    <ul class="tab">
        <li _type="1" style="text-align: center;flex:1;position: relative;border-right: solid 1px #efefef " class="active">全部订单<i class="on"></i></li>
        <li _type="2" style="text-align: center;flex:1;position: relative;border-right: solid 1px #efefef ">待支付<i></i></li>
        <li _type="3" style="text-align: center;flex:1;position: relative ">待评价<i></i></li>
    </ul>
    <input type="hidden" class="_type" value="1"/>
    <div style="width: 100%;height: 1rem;background: #f5f5f5"></div>
    <div class="warper">
        <!--全部订单-->
        <div style="margin-bottom: 3rem" class="allOrder">
            <!--全部订单-->
            <include file="Order:allOrder" />
        </div>
        <!--待支付-->
        <div class="paidOrder" style="margin-bottom: 3rem;display: none">

        </div>
        <!--待评价-->
        <div class="evaluatedOrder" style="margin-bottom: 3rem;display: none">
        </div>
    </div>
</div>
<include file="Public/fotter1" />

<script type="text/javascript" src="__PUBLIC__/js/jquery-3.2.1.min.js"></script>
<script src="/public/layer_mobile/layer.js"></script>
<script>
    $(function () {
        $(".tab li").click(function () {
            $(".tab li i").removeClass("on");
            $(".tab li").removeClass("active");
            $(this).addClass("active");
            $(this).children().addClass("on");
            var oIndex = $(this).index();
            $(".warper>div").eq(oIndex).show().siblings().hide();
            var type = $(this).attr('_type')
            $('._type').val(type)
            page = 1
            if(type == 1)
            {
                $.post("{:U('allOrder')}",{page:page},function (rel) {
                    $('.allOrder').html('')
                    $('.allOrder').append(rel)
                })
            }else if(type == 2)
            {
                //获取待支付订单
                $.post("{:U('paidOrder')}",{page:page},function (rel) {
                    //alert(rel)
                    $('.paidOrder').html('')
                    $('.paidOrder').append(rel)
                })
            }
            else if(type == 3)
            {
                //待评价订单
                $.post("{:U('evaluatedOrder')}",{page:page},function (rel) {
                    $('.evaluatedOrder').html('')
                    $('.evaluatedOrder').append(rel)
                })
            }
        })
    })
</script>
<script>
    var page = 1, //分页码
        off_on = false, //分页开关(滚动加载方法 1 中用的)
        timers = null; //定时器(滚动加载方法 2 中用的)
    //还可以基window窗口（body）来添加滚动事件, (因为布局不同,所以在这里没效果，因为[上面是基于body中的某个元素来添加滚动事的])
    $(window).scroll(function() {
        //当时滚动条离底部60px时开始加载下一页的内容
        if (($(window).height() + $(window).scrollTop() + 120) >= $(document).height()) {
            clearTimeout(timers);
            timers = setTimeout(function() {
                page++;
                //var select = $('#select').val()
                //alert(page)
                var _type = $('_type').val();
                if(type == 1)
                {
                    $.post("{:U('allOrder')}",{page:page},function (rel) {
                        /* if(rel.code == 200)
                         {
                             $('#he').append(rel.data)
                         }*/
                        $('.allOrder').append(rel)
                    })
                }else if(type == 2)
                {
                    //获取待支付订单
                    $.post("{:U('paidOrder')}",{page:page},function (rel) {
                        alert(rel)
                        $('.paidOrder').html('')
                        $('.paidOrder').append(rel)
                    })
                }
                else if(type == 3)
                {
                    //待评价订单
                    $.post("{:U('evaluatedOrder')}",{page:page},function (rel) {
                        alert(rel)
                        $('.evaluatedOrder').html('')
                        $('.evaluatedOrder').append(rel)
                    })
                }
            }, 300);
        }
    });
</script>
<script>
    //取消订单
    $(document).on('click','.cancel',function()
    {
        var order=$(this).attr('_order')
        layer.open({
            content: '您确定要取消订单吗？'
            ,btn: ['是', '否']
            ,yes: function(index){
                $.post("{:U('cancelOrder')}",{order:order},function (rels) {
                    if(rels == 0)
                    {
                        var _type = $('._type').val();
                        if(_type == 1)
                        {
                            $.post("{:U('allOrder')}",{page:1},function (rel) {
                                /* if(rel.code == 200)
                                 {
                                     $('#he').append(rel.data)
                                 }*/
                                $('.allOrder').html('')
                                $('.allOrder').append(rel)
                            })
                        }else if(_type == 2)
                        {
                            //获取待支付订单
                            $.post("{:U('paidOrder')}",{page:1},function (rel) {
                                $('.paidOrder').html('')
                                $('.paidOrder').append(rel)
                            })
                        }
                        else if(_type == 3)
                        {
                            //待评价订单
                            $.post("{:U('evaluatedOrder')}",{page:1},function (rel) {
                                $('.evaluatedOrder').html('')
                                $('.evaluatedOrder').append(rel)
                            })
                        }
                    }
                    else
                    {
                        layer.msg(rel)
                    }
                })
                layer.close(index);
            }
        });
    })


</script>
<script type="text/javascript">

    //调用微信JS api 支付
    function jsApiCall(order,price)
    {
    $.post("{:U('Weixin/get')}",{order:order,price:price},function(rels)
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
           rels,
            function(res){
                //WeixinJSBridge.log(res.err_msg);
                //alert(res.err_code+res.err_desc+res.err_msg);
                if(res.err_msg == "get_brand_wcpay_request:ok" ){
                    //使用以上方式判断前端返回,微信团队郑重提示：
                    //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                    alert('ok')
                }
            }
        );
    })

    }

    function callpay(order,price)
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall(order,price), false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall(order,price));
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall(order,price));
            }
        }else{
            jsApiCall(order,price);
        }
    }
</script>
</body>
</html>
